//
//  AppDelegate.swift
//  AirlineReservation
//
//  Created by Kevin on 2019/12/26.
//  Copyright © 2019 Kevin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var second = 5
    var timer:Timer?
    var window: UIWindow?
    // 后台任务标识
    var backgroundTask:UIBackgroundTaskIdentifier! = nil
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // 设置颜色
        setOrangeColor()
        // 创建window
        setRootWindow()
        // 根据通知切换根视图
        switchVc()
        return true
    }
    
    // 退出后台
    func applicationDidEnterBackground(_ application: UIApplication) {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(isExit), userInfo: nil, repeats: true)
        RunLoop.current.add(timer!, forMode: .common)
        backgroundTask = application.beginBackgroundTask(expirationHandler: nil)
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        stopTimer()
    }
    
    @objc func isExit() -> Void {
        if second > 1 {
            second -= 1
        }
        if second == 1 {
            stopTimer()
            exit(0)
        }
    }
    
    private func stopTimer() -> Void {
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
        if backgroundTask != nil {
            backgroundTask = nil
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: CUSTOM_SWITCH_NOTIFICATIONCENTER, object: nil)
    }
}

extension AppDelegate {
    
    // 根据通知切换根视图
    private func switchVc() -> Void {
        NotificationCenter.default.addObserver(forName: CUSTOM_SWITCH_NOTIFICATIONCENTER, object:nil, queue: OperationQueue.main) {[weak self] (notification) in
            if notification.object != nil ,"exit".elementsEqual(notification.object as! String){
                let vc = VCLogin()
                vc.isExit = true
                self?.window?.rootViewController =  UINavigationController(rootViewController: vc)
                return
            }
            let vc = notification.object != nil ? VCMain() : UINavigationController(rootViewController: VCLogin())
            self?.window?.rootViewController = vc
        }
    }
    
    // 设置为橘色
    func setOrangeColor() -> Void {
        UITabBar.appearance().tintColor = UIColor.orange
        UINavigationBar.appearance().tintColor = UIColor.orange
    }
    
    // 设置window
    func setRootWindow() -> Void {
        window = UIWindow()
        window?.frame = UIScreen.main.bounds
        window?.backgroundColor = UIColor.white
        window?.rootViewController = selectVc()
        window?.makeKeyAndVisible()
    }
    
    func selectVc() -> UIViewController {
        return UINavigationController(rootViewController: VCLogin())
    }
}
